window.onload = function () {
    const downLoadPDF = async () => {
        const sheets = spreadEnd.getActiveSheet();
        let rowCount = sheets.getRowCount();
        const liePd = getLiePd(spreadEnd, sourceDataEnd);
        const rowPd = getHangPd(spreadEnd, sourceDataEnd);
        let colCount = sheets.getColumnCount();
        if (rowPd && liePd) {
            rowCount = rowCount - rowPd - 1; //从0开始，多减去1
            colCount = colCount - liePd - 1;
        }
        const resp = await getMXValue(getFiledList(spreadEnd, sourceDataEnd), rowCount, colCount, "1");
        if (!(resp.data.length > 0)) {
            message.error("暂无数据打印");
            return;
        }
        const data = resp.data[0].data[0];
        const pdf = new jsPDF("l", "mm", "a4");
        pdf.setFont("simkai");
        pdf.setFontSize(12);
        pdf.text("附件1", 10, 10);
        pdf.setFontSize(28);
        pdf.text("2023年度中央行政事业单位国有资产决算报表", 50, 30);
        pdf.setFontSize(12);
        pdf.text("单  位  公  章", 140, 50);
        pdf.setLineWidth(0.1);
        const colNames = [
            "单位名称：",
            data.COVERG1_1,
            "单位负责人：",
            data.COVERG2_1,
            "资产管理负责人：",
            data.COVERG3_1,
            "填表人：",
            data.COVERG4_1,
            "电话号码：",
            data.COVERG5_1 + "-" + data.COVERG6_1,
            "单位地址：",
            data.COVERG8_1,
            "邮政编码：",
            data.COVERG10_1,
            "编制日期：",
            data.COVERG23_1,
        ];
        const phoneCol = ["电话号码：", "", "电话号码：", ""];
        let y = 60;
        let x = 83;
        for (let i = 0; i < colNames.length; i++) {
            if (colNames[i] == "null" || colNames[i] == undefined || colNames[i] == "NULL") {
                colNames[i] = "";
            }
            if (i % 2 === 0) {
                pdf.text(colNames[i], x, y + i * 5);
            } else {
                if (i == colNames.length - 1) {
                    console.log(colNames[i]);
                    pdf.text(colNames[i].length > 0 ? colNames[i].substring(0, 10) : colNames[i], x + 32, y + (i - 1) * 5);
                } else {
                    pdf.text(colNames[i], x + 32, y + (i - 1) * 5);
                }
                if (i == 5 || i == 7) {
                    pdf.line(x + 32, y + (i - 1) * 5 + 2, x + 72, y + (i - 1) * 5 + 2);
                } else {
                    pdf.line(x + 32, y + (i - 1) * 5 + 2, x + 130, y + (i - 1) * 5 + 2);
                }
            }
        }
        for (let i = 0; i < phoneCol.length; i++) {
            if (phoneCol[i] == "null" || phoneCol[i] == undefined || phoneCol[i] == "NULL") {
                phoneCol[i] = "";
            }
            if (i % 2 === 0) {
                pdf.text(phoneCol[i], x + 75, y + 21 + i * 5);
            } else {
                if (i == colNames.length - 1) {
                    pdf.text(phoneCol[i].length > 0 ? phoneCol[i].substring(0, 9) : phoneCol[i], x + 95, y + 21 + (i - 1) * 5);
                } else {
                    pdf.text(phoneCol[i], x + 95, y + 21 + (i - 1) * 5);
                }

                pdf.line(x + 95, y + 20 + (i - 1) * 5 + 2, x + 130, y + 20 + (i - 1) * 5 + 2);
            }
        }
        pdf.text("(签章)", x + 117, y + 11);
        pdf.text("(签章)", x + 59, y + 21);
        pdf.text("(签章)", x + 59, y + 31);

        // 定义表格的列数和行数
        const columns = ["col1", "col2"];
        const datas = [
            ["统一社会信用代码：" + data.COVERG11_1, "单位性质：" + data.COVERG14_1],
            ["财政预算代码：" + data.COVERG12_1, "单位执行会计制度：" + data.COVERG15_1],
            ["部门标识代码：" + data.COVERG24_1, "事业单位改革分类：" + data.COVERG21_1],
            ["单位所在地区（国家标准：行政区划代码）：" + data.COVERG13_1, "行业类型：" + data.COVERG16_1],
            ["上年代码：" + data.COVERG17_1, "新报因素：" + data.COVERG19_1],
        ];

        // 定义表格的起始位置和样式
        let startY = 130;
        let rowHeight = 10;
        let colWidth = 137;
        let borderWidth = 0.1;

        // 绘制表格内容及边框
        for (let j = 0; j < datas.length; j++) {
            for (let k = 0; k < columns.length; k++) {
                pdf.setDrawColor(0);
                pdf.setLineWidth(borderWidth);
                pdf.rect(10 + k * colWidth, startY + (j + 1) * rowHeight, colWidth, rowHeight);
                pdf.text(12 + k * colWidth, startY + (j + 1) * rowHeight + 7, datas[j][k].toString());
            }
        }

        // 保存或展示生成的PDF
        pdf.save("封面表.pdf");
    };
};
